package br.com.ufc.apsoo.hotelar.dao.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import br.com.ufc.apsoo.hotelar.dao.ApartamentoDao;
import br.com.ufc.apsoo.hotelar.model.Apartamento;
import br.com.ufc.apsoo.hotelar.model.TipoApartamento;
import br.com.ufc.apsoo.hotelar.utils.HibernateUtil;

/**
 * @author Virginia
 * 
 */
public class ApartamentoDaoImpl implements ApartamentoDao {

	@Override
	public void save(Apartamento apartamento) {
		Session session = HibernateUtil.getSession();
		Transaction t = session.beginTransaction();
		session.save(apartamento);
		t.commit();
		session.close();
	}

	@Override
	public void remove(Apartamento apartamento) {
		Session session = HibernateUtil.getSession();
		Transaction t = session.beginTransaction();
		session.delete(apartamento);
		t.commit();
		session.close();
	}

	@Override
	public void update(Apartamento apartamento) {
		Session session = HibernateUtil.getSession();
		Transaction t = session.beginTransaction();
		session.update(apartamento);
		t.commit();
		session.close();
	}

	@Override
	public Apartamento getApartamentoById(int id) {
		Session session = HibernateUtil.getSession();
		try {
			return (Apartamento) session.get(Apartamento.class, id);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Apartamento> list() {
		Session session = HibernateUtil.getSession();
		try {
			return session.createCriteria(Apartamento.class).list();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Apartamento> getApartamentosDisponiveis() {
		Session session = HibernateUtil.getSession();
		String query = "from Apartamento where status = true";
		try {
			return session.createQuery(query).list();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Apartamento> getApartamentoByTipoAndDisponivel(
			TipoApartamento tipo) {
		Session session = HibernateUtil.getSession();
		String query = "from Apartamento where tipo = :tipo and status = true";
		try {
			return session.createQuery(query).setInteger("tipo", tipo.getId())
					.list();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Apartamento> getApartamentoByTipoAndDisponivelNoPeriodo(
			TipoApartamento tipo, Date checkin, Date checkout) {
		Session session = HibernateUtil.getSession();
		String query = "from Apartamento a where a.tipo = :tipo and status = true and a.id not in (select r.apartamento from Reserva r where r.status = true and ((r.periodo.inicio BETWEEN :checkin AND :checkout) or (r.periodo.fim BETWEEN :checkin AND :checkout) or (:checkin BETWEEN r.periodo.inicio AND r.periodo.fim)))";
		try {
			return session.createQuery(query).setInteger("tipo", tipo.getId()).setDate("checkin", checkin).setDate("checkout", checkout)
					.list();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return null;
	}

}
